from django.views import View
# 导入返回响应
from SaaSDevelTool.data_return_response import create_response
# 导入错误信息装饰器
from SaaSDevelTool.data_return_response import error_message_detection
# 查询当前参数是否是int类型
from SaaSDevelTool.data_operation import is_number
# 获取分页器
from SaaSDevelTool.data_operation import paging_tool_join

"""展示用户组"""


class SaaSDevelRoleRbacQuery(View):
    @error_message_detection
    def get(self, request):
        # 获取当前页码
        page_number = request.GET.get("page_number")
        # 获取当前每页展示的数据条数
        page_size = request.GET.get("page_size")
        # 接收名称
        role_name = request.GET.get("role_name")

        # 判断参数是否是int类型
        if not is_number(page_number) and not is_number(page_size):
            return create_response(code=400, msg="页码于条数必须是数字类型。")

        # 判断user_name是否存在
        if role_name:
            # 定义sql语句
            where_role_name_sql = """ WHERE role_name LIKE '%{}%' """.format(role_name)
        else:
            where_role_name_sql = """"""

        # 定义查询语句
        query_base_info_role_table_sql = """
            SELECT 
                r.role_id,
                r.role_name,
                DATE_FORMAT(r.create_time,'%Y-%m-%d %H:%i:%s') as create_time,
                DATE_FORMAT(r.update_time,'%Y-%m-%d %H:%i:%s') as update_time,
                user.user_name as create_user_name,
                r.role_status
            FROM 
                db_center_rbac.base_info_role_table r 
            JOIN 
                db_center_rbac.base_info_user_table user  
            ON  
                r.create_user = user.user_id {} 
            ORDER BY 
                r.role_id 
            DESC 
        ;""".format(where_role_name_sql)

        # 调用分页器
        role_message_data = paging_tool_join(query_base_info_role_table_sql, int(page_number), int(page_size))

        # 返回对应的数据
        return create_response(code=200, msg="", data=role_message_data)
